﻿<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">

        function GetYoutubeUrl(input) {
            var formats = GetAvailableFormats(input);
            window.external.notify(formats[18].toLocaleString());
        }

        function GetHDYoutubeUrl(input) {

            var formats = GetAvailableFormats(input);

            if ('22' in formats)
                window.external.notify(formats[22].toLocaleString());
            else
                window.external.notify(formats[18].toLocaleString());


        }

        function GetAvailableFormats(input) {
            var patt = /url_encoded_fmt_stream_map.*?",/g;
            var fm_stream_map_encoded = patt.exec(input);
            fm_stream_map_encoded = fm_stream_map_encoded.toLocaleString();
            fm_stream_map_encoded = fm_stream_map_encoded.slice(30, -2); //remove "url_encoded_fmt_stream_map": and trailing ",

            //var result = unescape(fm_stream_map_encoded);

            var formatsGroup = fm_stream_map_encoded.split(",");

            var videoURL = new Array();

            for (var i = 0; i < formatsGroup.length; i++) {
                var videoFormatsElem = formatsGroup[i].split("\\u0026");

                var videoFormatsPair = new Array();

                for (var j = 0; j < videoFormatsElem.length; j++) {
                    var pair = videoFormatsElem[j].split("=");
                    if (pair.length == 2) {
                        videoFormatsPair[pair[0]] = pair[1];
                    }
                }

                var url = (videoFormatsPair['url']) ? videoFormatsPair['url'] : null;
                if (url == null) continue;
                url = unescape(unescape(url)).replace(/\\\//g, '/').replace(/\\u0026/g, '&');
                var itag = (videoFormatsPair['itag']) ? videoFormatsPair['itag'] : null;
                if (itag == null) continue;
                var signature = (videoFormatsPair['sig']) ? videoFormatsPair['sig'] : null;
                if (signature != null) {
                    url = url + "&signature=" + signature;
                }
                if (url.toLowerCase().indexOf('http') == 0) { // validate URL
                    videoURL[itag] = url;
                }
            }

            return videoURL;
        }
		
    </script>
</head>
<body>
</body>
</html>